<template>
  <div class="home-search-box">
    <div class="item location bottom-gray-line">
      <div class="city" @click="cityPageClick">{{ currentCity.cityName }}</div>
      <div class="position" @click="positionClick">
        <span class="text">我的位置</span>
        <img src="@/assets/img/home/icon_location.png" alt="" />
      </div>
    </div>

    <div class="item date-range bottom-gray-line" @click="unfold">
      <div class="start">
        <span class="text">入住</span>
        <span class="date">{{ startDateStr }}</span>
      </div>
      <div class="stay">共{{ stayCount }}晚</div>
      <div class="end">
        <span class="text">离店</span>
        <div class="date">{{ endDateStr }}</div>
      </div>
    </div>
    <van-calendar
      v-model:show="showCalendar"
      type="range"
      @confirm="onConfirm"
      color="var(--primary-color)"
    />

    <div class="item price-counter bottom-gray-line">
      <div class="start">价格不限</div>
      <div class="end">人数不限</div>
    </div>
    <div class="item keyword bottom-gray-line">关键字/位置/民宿</div>

    <div class="item hot-suggests">
      <template v-for="(item, index) in hotSuggests" :key="index">
        <div class="suggest-item">{{ item.tagText.text }}</div>
      </template>
    </div>

    <div class="item search">
      <div class="btn" @click="searchClick">开始搜索</div>
    </div>
  </div>
</template>

<script setup>
import { ref, computed } from "vue";
import { useRouter } from "vue-router";
import { storeToRefs } from "pinia";

import useCityStore from "@/stores/modules/city";
import useHomeStore from "@/stores/modules/home";
import useMainStore from "@/stores/modules/main"

import { formatMonthDay, getDiffDays } from "@/utils/format_date";

// 获取位置/城市
const positionClick = () => {
  navigator.geolocation.getCurrentPosition(
    (res) => {
      console.log("获取位置成功", res);
    },
    (err) => {
      console.log("获取位置失败", err);
    },
    {
      enableHighAccuracy: true,
      timeout: 1000,
      maximumAge: 0,
    }
  );
};

// 跳转至城市页面
const router = useRouter();
const cityPageClick = () => {
  router.push("/city");
};

// 当前城市(store)
const cityStore = useCityStore();
const { currentCity } = storeToRefs(cityStore);

// 入住/离店时间
const mainStore = useMainStore()
const { startDate, endDate } = storeToRefs(mainStore)

const startDateStr = computed(() => formatMonthDay(startDate.value))
const endDateStr = computed(() => formatMonthDay(endDate.value))

const stayCount = ref(getDiffDays(startDate.value, endDate.value))

// 日历
const showCalendar = ref(false);

const unfold = () => {
  showCalendar.value = true;
}

const onConfirm = (value) => {
  const selectStartDate = value[0];
  const selectEndDate = value[1];

  mainStore.startDate = selectStartDate
  mainStore.endDate = selectEndDate
  stayCount.value = getDiffDays(selectStartDate, selectEndDate);

  showCalendar.value = false
};

// 热门建议(store数据)
const HomeStore = useHomeStore()
const { hotSuggests } = storeToRefs(HomeStore)

// 搜索按钮
const searchClick = () => {
  router.push({
    path: "/search",
    query: {
      startDate: startDate.value,
      endDate: endDate.value,
      currentCity: currentCity.value.cityName
    }
  })
}
</script>

<style lang="less" scoped>
.home-search-box {
  .item {
    display: flex;
    align-items: center;
    flex-wrap: wrap;
    height: 44px;
    padding: 0 20px;
  }

  .location {
    .city {
      flex: 1;
      font-size: 15px;
      color: #333;
    }

    .position {
      display: flex;
      align-items: center;

      .text {
        font-size: 12px;
        color: #666;
        margin-right: 8px;
      }

      img {
        width: 18px;
        height: 18px;
      }
    }
  }

  .date-range {
    font-size: 12px;

    .start {
      display: flex;
      flex-direction: column;
    }

    .stay {
      flex: 1;
      display: flex;
      align-items: center;
      justify-content: center;

      color: #666;
    }

    .end {
      min-width: 30%;
    }

    .text {
      color: #999;
    }

    .date {
      margin-top: 5px;
      font-size: 15px;
      color: #333;
    }
  }

  .price-counter {
    font-size: 14px;
    color: #999;
    .start {
      flex: 1;

      border-right: 1px solid #f8f9f9;
    }

    .end {
      min-width: 30%;
      padding-left: 20px;
    }
  }

  .keyword {
    font-size: 14px;
    color: #999;
  }

  .hot-suggests {
    margin: 10px 0;
    height: auto;

    .suggest-item {
      font-size: 12px;
      color: #3f4954;
      background-color: #f1f3f5;
      padding: 4px 8px;
      margin: 3px;
      border-radius: 14px;
    }
  }

  .search {
    .btn {
      width: 342px;
      height: 38px;
      max-height: 50px;
      line-height: 38px;
      font-weight: 500;
      font-size: 18px;
      color: #fff;
      border-radius: 20px;
      text-align: center;
      background-image: var(--theme-linear-gradient);
    }
  }
}
</style>
